package controllers

import (
	"data_summary/common/app"
	db2 "data_summary/common/db"
	"data_summary/common/models"
	"data_summary/contrab"
	"data_summary/service/dao"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 手动运行任务
func Run(ctx *gin.Context) {
	id := ctx.Param(":id")
	Db := db2.GetDb("database")
	daDb := dao.NewTaskDao(Db)
	task, err := daDb.GetTask(map[string]interface{}{"id": id})
	if err != nil || task.Id <= 0 {
		app.JsonResponse(ctx, http.StatusOK, app.ERROR, err)
	}

	task.Spec = "手动运行"
	taskDo := contrab.NewTask()
	taskDo.Run(task)
	app.JsonResponse(ctx, http.StatusOK, app.SUCCESS,
		gin.H{
			"msg": "任务已开始运行, 请到任务日志中查看结果",
		},
	)
}

// 解析查询参数
func parseQueryParams(ctx *gin.Context) models.CommonMap {
	var params models.CommonMap = models.CommonMap{}
	params["Id"] = ctx.Param("id")
	params["HostId"] = ctx.Param("host_id")
	params["Name"] = ctx.Param("name")
	params["Protocol"] = ctx.Param("protocol")
	params["Tag"] = ctx.Param("tag")
	statusStr := ctx.Param("status")
	status, _ := strconv.Atoi(statusStr)
	if status >= 0 {
		status -= 1
	}
	params["Status"] = status
	return params
}
